package com.linln.agent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.linln.admin.system.controller.UploadController;
import com.linln.common.constant.AdminConst;
import com.linln.common.data.URL;
import com.linln.common.enums.ResultEnum;
import com.linln.common.exception.ResultException;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.MD5;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.SpringContextUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.service.MemberAgentService;
import com.linln.modules.member.service.MemberInfoService;
import com.linln.modules.member.service.MemberOrderService;
import com.linln.modules.message.service.MessageInfoService;
import com.linln.modules.store.service.StoreinfoService;
import com.linln.modules.system.domain.Upload;
import com.linln.modules.system.domain.User;
/**
 * @author 小懒虫
 * @date 2018/8/14
 */
@Controller
public class AgentMainController{

    @Autowired
    private MemberInfoService memberService;
    @Autowired
    private MemberAgentService agentService;
  
    @Autowired
    private MessageInfoService messageService;
    @Autowired
    private StoreinfoService storeService;

    @Autowired
    private MemberOrderService orderService;

    /**
     * 后台主体内容
     */
    @GetMapping("/agent")
    public String agentmain(Model model,HttpServletRequest request,String area){
    	 MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME); 
         model.addAttribute("agent", agent);
         MemberAgent memberagent=agentService.getByMemberIdAndArea(agent.getId(), area);
         model.addAttribute("agent", agent);
         model.addAttribute("memberagent", memberagent);
         HttpSession session=request.getSession();
         session.setAttribute(AdminConst.AGENT_AREA_NAME, memberagent); 
       
          return "/agent/main";
    }

    /**
     * 主页
     */
    @GetMapping("/agent/index")
    public String index(Model model,HttpServletRequest request){
    	 MemberAgent memberagent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME); 
    	 //代理商查看实时数据 
    	int membercount=memberService.getCountByArea(memberagent.getArea());
    	int messagecount=messageService.getCountByArea(memberagent.getArea());
    	int storecount=storeService.getCountByArea(memberagent.getArea());
    	double allmoney=orderService.getSumByArea(memberagent.getArea());
        model.addAttribute("membercount", membercount);
        model.addAttribute("messagecount", messagecount);
        model.addAttribute("storecount", storecount);
        model.addAttribute("allmoney", allmoney);

        return "/agent/main/index";
    }


    /**
     * 跳转到个人信息页面
     */
    @GetMapping("/agent/userInfo")
    public String toUserInfo(Model model,HttpServletRequest request){
    //    User user = ShiroUtil.getSubject();
        MemberAgent memberagent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME); 
        model.addAttribute("user", memberagent.getMember());
        return "/agent/main/userInfo";
    }

    

    /**
     * 保存修改个人信息
     */
    @PostMapping("/agent/userInfo")
    @ResponseBody
    public ResultVo userInfo(MemberInfo memberInfo){

        // 复制保留无需修改的数据
         MemberInfo smemberInfo=new MemberInfo();
        String[] fields = {"id", "nickname", "password", "salt", "picture"};
        EntityBeanUtil.copyProperties(smemberInfo, memberInfo, fields);

        // 保存数据
        memberService.save(memberInfo);
        return ResultVoUtil.success("保存成功", new URL("/agent/userInfo"));
    }

    /**
     * 跳转到修改密码页面
     */
    @GetMapping("/agent/editPwd")
     public String toEditPwd(){
        return "/agent/main/editPwd";
    }

    /**
     * 保存修改密码
     */
    @PostMapping("/agent/editPwd")
     @ResponseBody
    public ResultVo editPwd(String original,HttpServletRequest request, String password, String confirm){
        // 判断原来密码是否有误
    	
        MemberAgent memberagent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME); 
        MemberInfo memberInfo=memberagent.getMember();
        
        
        String oldPwd =MD5.getMD5String(original);
        if (original.isEmpty() || "".equals(original.trim()) || !oldPwd.equals(memberInfo.getPassword())) {
            throw new ResultException(ResultEnum.USER_OLD_PWD_ERROR);
        }

        // 判断密码是否为空
        if (password.isEmpty() || "".equals(password.trim())) {
            throw new ResultException(ResultEnum.USER_PWD_NULL);
        }

        // 判断两次密码是否一致
        if (!password.equals(confirm)) {
            throw new ResultException(ResultEnum.USER_INEQUALITY);
        }

        // 修改密码，对密码进行加密
        memberInfo.setPassword(MD5.getMD5String(password));
        // 保存数据
        memberService.save(memberInfo);
        return ResultVoUtil.success("修改成功");
    }
}
